import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { UserModule } from './user/user.module';
import defaultConfig from './config/default.config'

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [defaultConfig],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        const { host, database, username, password, port, charset } = configService.get('db')

        return {
          type: 'mysql',
          username: username,
          password: password,
          host: host,
          port: port,
          database: database,
          entities: [],
          synchronize: true,
          retryDelay: 500,
          retryAttempts: 10,
          autoLoadEntities: true,
          timezone: '+08:00',
          charset: charset,
          // logging: ['query', 'error'],
        }
      },
    }),
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
